package com.situ.vivamarket.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.situ.vivamarket.dao.AddressDao;
import com.situ.vivamarket.dao.CartItemDao;
import com.situ.vivamarket.dao.OrderDao;
import com.situ.vivamarket.model.*;
import com.situ.vivamarket.model.front.CartItem;
import com.situ.vivamarket.model.front.OrderItem;
import com.situ.vivamarket.model.search.OrderSearchBean;
import com.situ.vivamarket.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao;
    private CartItemDao cartItemDao;
    private AddressDao addressDao;


    @Autowired
    public void setOrderDao(OrderDao orderDao) {
        this.orderDao = orderDao;
    }
    @Autowired
    public void setCartItemDao(CartItemDao cartItemDao) {
        this.cartItemDao = cartItemDao;
    }
    @Autowired
    public void setAddressDao(AddressDao addressDao) {
        this.addressDao = addressDao;
    }

    //结算页面时生成订单
    @Transactional(rollbackFor = Exception.class)//所有SQL要么同时成功，要么同时失败,失败会回滚
    @Override
    public boolean checkout(Order order, Integer[] ids) {
        if (ids.length == 0 || ids == null) {
            return false;
        }
//        创建订单
        int count = this.orderDao.insert(order);
        if (count == 0) {
            throw new RuntimeException("保存订单异常");

        }
//        保存订单项
        for (Integer id : ids) {
            CartItem ci = new CartItem();
            ci.setId(id);
            ci = cartItemDao.findByUnique(ci);
            Goods goods = ci.getGoods();
            if (goods == null) {
                throw new RuntimeException("购买的商品不存在");
            }

            OrderItem oi = new OrderItem();
            oi.setOrderId(order.getId());//订单号
            oi.setGoodsId(goods.getId());//商品编号
            oi.setDealPrice(goods.getPrice());//成交价格
            oi.setQuantity(ci.getQuantity());//购买的商品数量
            oi.setGoodsName(goods.getName());
            oi.setGoodsPic(goods.getPic());
            oi.setGoodsSummary(goods.getSummary());
            count = this.orderDao.insertOrderItem(oi);
            if (count == 0) {
                throw new RuntimeException("保存订单项异常");
            }

        }
//        删除购物车
        this.cartItemDao.deleteByIds(ids);

        return true;
    }


//    创建订单项
    @Override
    public boolean insertOrderItem(OrderItem oi) {
        return orderDao.insertOrderItem(oi) > 0;
    }
//根据id查找订单
    @Override
    public Order findById(Integer id) {
        return orderDao.findById(id);
    }

//    部分修改订单，收获信息，总价，支付方式

    @Override
    public boolean patch(Order order) {
        return this.orderDao.patch(order) > 0;
    }
//      根据订单项查找订单
    @Override
    public Order findByOrderNo(String orderNo) {
        return this.orderDao.findByOrderNo(orderNo);
    }
//   查找用户最近一笔未支付订单
    @Override
    public Order findLastCreatedOrderByMemberId(Integer memberId) {
        return orderDao.findLastCreatedOrderByMemberId(memberId);
    }
//    查找用户所有订单

    @Override
    public List<Order> findByMemberId(Integer memberId) {
        return orderDao.findByMemberId(memberId);
    }
//  查找所有订单
    @Override
    public List<Order> getAllOrders(OrderSearchBean osb, Page<?> page) {
        try (Page<?> __ = PageHelper.startPage(page.getPageNum(), page.getPageSize())) {
            return orderDao.findAll(osb);
        }
    }
//    根据id批量删除订单
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByIds(Integer[] ids) {
        //先删除订单项，写操作
        this.orderDao.deleteOrderItemsByOrderIds(ids);

        //再删除订单，写操作
        return this.orderDao.deleteByIds(ids);
    }


    //  根据address的id查找全名
    @Override
    public String findFullNameById(Integer id) {
        return addressDao.findFullNameById(id);
    }
}
